//
//  ReferralNetworkController.swift
//  SCA
//
//  Created by tjun on 2025/9/21.
//

import UIKit



struct ReferralNetworkModel {
    let Id: String
    let title: String
    let account: String
}

struct MatrixNetworkModel {
    let Id: String
    let isMain: Bool
    let powerDisp: String
    let power: String
    let investment: String
    
    init(Id: String, dict: [String: Any]) {
        self.Id = Id
        self.isMain = dict.boolValue("is_main")
        self.powerDisp = dict.stringValue("power_disp")
        self.power = dict.stringValue("power")
        self.investment = dict.stringValue("investment")
    }
}

class ReferralNetworkController: BaseController {
    
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 16
        layout.minimumInteritemSpacing = 0
        
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 16
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 40)
        layout.itemSize = CGSize(width: 180, height: 52)
        
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = AppStyle.color.background
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.alwaysBounceVertical = true
        collectionView.showsVerticalScrollIndicator = false
        collectionView.contentInsetAdjustmentBehavior = .never
        collectionView.register(ReferralNetworkCell.self, forCellWithReuseIdentifier: ReferralNetworkCell.identifier)
        return collectionView
    }()
    
    private let mainNetworkLabel = UILabel(font: UIFontMake(14), textColor: .white, textAlignment: .center)
    
    private var datas: [ReferralNetworkModel] = []
        
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        let contentView = UIView().addTo(self.view)
        contentView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(navBarHeight + 30)
            make.height.equalTo(screenHeight - navBarHeight - 30 - tabBarSafeBottomMargin)
        }
        
        let mainNetworkView = UIImageView()
        mainNetworkView.addTo(contentView)
        mainNetworkView.image = UIImage(named: "referralNetwork_bg")
        mainNetworkView.snp.makeConstraints { make in
            make.width.height.equalTo(50)
            make.centerY.equalToSuperview()
            make.left.equalTo(AppMargin.left + 10)
        }
        
        mainNetworkLabel.addTo(mainNetworkView)
        mainNetworkLabel.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let mainNetworkArrowView = UIImageView()
        mainNetworkArrowView.addTo(contentView)
        mainNetworkArrowView.image = UIImage(named: "referralNetwork_arror")
        mainNetworkArrowView.snp.makeConstraints { make in
            make.centerY.equalTo(mainNetworkView)
            make.left.equalTo(mainNetworkView.snp.right).offset(20)
        }
        
        collectionView.addTo(contentView)
        collectionView.snp.makeConstraints { make in
            make.left.equalTo(mainNetworkArrowView.snp.right)
            make.top.bottom.equalToSuperview()
            make.right.equalToSuperview()
        }
        
        MineService.loadNetworkSponsorData(success: { models in
            self.datas.removeAll()
            self.datas.append(contentsOf: models)
            let firstModel = self.datas.removeFirst()
            self.mainNetworkLabel.text = firstModel.title
            self.collectionView.reloadData()
            self.reloadLayout()
        })
        
        reloadLayout()
    }
    
    func reloadLayout() {
        collectionView.layoutIfNeeded()
        DispatchQueue.main.async {
            let contentHeight = self.collectionView.collectionViewLayout.collectionViewContentSize.height
            let collectionViewHeight = self.collectionView.bounds.height
            let verticalInset = max(0, (collectionViewHeight - contentHeight) / 2)
            self.collectionView.contentInset = UIEdgeInsets(top: verticalInset, left: 0, bottom: verticalInset > 0 ? 0 : tabBarSafeBottomMargin, right: 0)
        }
    }
    
}


extension ReferralNetworkController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return datas.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let model = datas[indexPath.row]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ReferralNetworkCell.identifier, for: indexPath) as! ReferralNetworkCell
        cell.model = model
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        
    }
    
}
